﻿using System;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using RuleCode.Table;

namespace map
{
    public partial class area5info : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //bind user bizarea
            if (!IsPostBack)
            {
                Fck_Content.Visible = false;
                SubmitEdit.Visible = false;
                fck_title.Visible = false;
                GridViewBind();
                areaLogo.ImageUrl = "/img/upItem.gif";
            }
        }
        private void GridViewBind()
        {
            RuleCode.Table.BizArea x = RuleCode.Table.BizArea.Find("CreatedBy = '" + AliApi.appUserId + " '");
            this.GridView1.DataSource = x.MyTable;
            this.GridView1.DataBind();

        }
        private void DataListBind(string id)
        {
            BizAreaImages image = BizAreaImages.Find("BizAreaID = " + id);
            this.DataList1.DataSource = image.MyTable;
            this.DataList1.DataBind();
            
        }
        private BizArea biz;
        protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            string id = GridView1.DataKeys[e.NewSelectedIndex].Value.ToString();
            Label1.Text = id;
            biz = BizArea.Find(" recid = '" + id + "'", "RECID");
            
            //desp.Text = biz.BizAreaDescription.ValueString;
            //transparent.Text = biz.BizAreaTransparent.ValueString;
            FCKeditor_desk.Value = biz.BizAreaDescription.ValueString;
            FCKeditor_transparent.Value = biz.BizAreaTransparent.ValueString;
            areaLogo.ImageUrl = (biz.BizAreaImagePath.StringValue == "" ? "/img/upItem.gif" : biz.BizAreaImagePath.StringValue);

            TextBox2.Text = biz.p1.ToString();
            TextBox4.Text = biz.p2.ToString();
            TextBox6.Text = biz.p3.ToString();

            DataListBind(id);

           
        }
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
            BizArea uc = BizArea.Find(" recid = '" + id + "'", "RECID");
            BizAreaImages i = BizAreaImages.Find(" BizAreaID = '" + id + "'", "BizAreaID");
            //while (i.Next())
            //{

            i.Delete(" BizAreaID = '" + id + "'");
            //}
            uc.Delete();
            GridViewBind();
            DataListBind(Label1.Text);
        }
        protected void editDesp_Click(object sender, EventArgs e)
        {
            textSource.Text = "BizAreaDescription";
            fck_title.Text = "地标介绍:";
            Edit(desp.Text);
        }

        protected void editTransparent_Click(object sender, EventArgs e)
        {
            textSource.Text = "BizAreaTransparent";
            fck_title.Text = "地标配套:";
            Edit(transparent.Text);
        }
        private void Edit(string source)
        {
            Fck_Content.Visible = true;
            SubmitEdit.Visible = true;
            fck_title.Visible = true;
            Fck_Content.Value = Server.HtmlDecode(source);

        }

        protected void SubmitEdit_Click(object sender, EventArgs e)
        {
            Fck_Content.Visible = false;
            SubmitEdit.Visible = false;
            fck_title.Visible = false;
            biz = BizArea.Find(" recid = '" + Label1.Text + "'", "RECID");

            if (textSource.Text == "BizAreaDescription")
            {
                biz.BizAreaDescription = Fck_Content.Value;
                desp.Text = Fck_Content.Value;
            }
            if (textSource.Text == "BizAreaTransparent")
            {
                biz.BizAreaTransparent = Fck_Content.Value;
                transparent.Text = Fck_Content.Value;
            }
            biz.Modify();
        }

        protected void BTNUp_Click(object sender, EventArgs e)
        {
            //string root = Server.MapPath("~/");
            //string path = "pic/";
            //string mpath = path + FileUpload1.FileName;
            bool fileOK = false;

            if (FileUpload1.HasFile)
            {
                //返回文件的扩展名
                String fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
                //设置限制的文件类型
                String[] allowExtension = { ".jpg", ".gif", ".bmp" ,".png"};
                //判断用户选择的文件类型是否受限制
                for (int i = 0; i < allowExtension.Length; i++)
                {
                    if (fileExtension == allowExtension[i])
                    {
                        fileOK = true;
                    }
                }
                //大于1M的文件不能上传
                if (FileUpload1.PostedFile.ContentLength > 1024000)
                {
                    fileOK = false;
                }

            }
            string strImg2 = FileUpload1.FileName.Substring(FileUpload1.FileName.LastIndexOf(".") + 1);
            string time2 = DateTime.Now.ToString("yyyyMMddhhmmss") + "." + strImg2;
            string strFilePth = Server.MapPath(chatblog.Util.UploadImageDirectory) + time2;
            string linkpth = ResolveUrl(chatblog.Util.UploadImageDirectory) + time2;
            //if (FileUpload_fj.HasFile)
            //{
            //    FileUpload_fj.SaveAs(strFilePth);
            //}

            //if (Confirm.Confirms(mpath) == true)//调用Confirm.Confirms()验证图片是否已经存在
            //{

            //    Response.Write("<script language=javascript>alert('该图片已存在!');</script>");

            //}
            if (!fileOK)
            {
                Response.Write("<script>alert('图片格式不正确对或者超过1M！');</script>");
            }
            if ( fileOK)
            {
                FileUpload1.PostedFile.SaveAs(strFilePth);
                Response.Write("<script>alert('上传成功！');</script>");
                RuleCode.Table.BizAreaImages images = BizAreaImages.Find(" 1=2 ");
                images.New();
                images.BizAreaID = Label1.Text;
                images.BizAreaImagePath = linkpth;
                images.BizAreaImageDescription = imageDespcripton.Text;
                images.Insert();
                DataListBind(Label1.Text);

               // Response.Redirect("area5info.aspx");

            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            biz = BizArea.Find(" recid = '" + Label1.Text + "'", "RECID");
            biz.p1.Value = TextBox2.Text;
            biz.p2.Value = TextBox4.Text;
            biz.p3.Value = TextBox6.Text;
            biz.Modify();
            Label2.Text = "修改成功。";
        }

        protected void TextBox2_TextChanged(object sender, EventArgs e)
        {

        }

        protected void BTNUp1_Click(object sender, EventArgs e)
        {
            bool fileOK = false;
            if (string.IsNullOrEmpty(Label1.Text))
            {
                Response.Write("<script>alert('请选择地标1M！');</script>");
                
            }
            else
            {
                
            

            if (FileUpload2.HasFile)
            {
                //返回文件的扩展名
                String fileExtension = System.IO.Path.GetExtension(FileUpload2.FileName).ToLower();
                //设置限制的文件类型
                String[] allowExtension = { ".jpg", ".gif", ".bmp", ".png" };
                //判断用户选择的文件类型是否受限制
                for (int i = 0; i < allowExtension.Length; i++)
                {
                    if (fileExtension == allowExtension[i])
                    {
                        fileOK = true;
                    }
                }
                //大于1M的文件不能上传
                if (FileUpload2.PostedFile.ContentLength > 1024000)
                {
                    fileOK = false;
                }

            }
            string strImg2 = FileUpload2.FileName.Substring(FileUpload1.FileName.LastIndexOf(".") + 1);
            string time2 = DateTime.Now.ToString("yyyyMMddhhmmss") + "." + strImg2;
            string strFilePth = Server.MapPath(chatblog.Util.UploadImageDirectory) + time2;
            string linkpth = ResolveUrl(chatblog.Util.UploadImageDirectory) + time2;
            //if (FileUpload_fj.HasFile)
            //{
            //    FileUpload_fj.SaveAs(strFilePth);
            //}

            //if (Confirm.Confirms(mpath) == true)//调用Confirm.Confirms()验证图片是否已经存在
            //{

            //    Response.Write("<script language=javascript>alert('该图片已存在!');</script>");

            //}
            if (!fileOK)
            {
                Response.Write("<script>alert('图片格式不正确对或者超过1M！');</script>");
            }
            if (fileOK)
            {
                FileUpload2.PostedFile.SaveAs(strFilePth);
                Response.Write("<script>alert('上传成功！');</script>");
               
              var  x = BizArea.Find(" recid = '" + Label1.Text + "'", "RECID");
                x.BizAreaImagePath = linkpth;
                x.Modify();
                areaLogo.ImageUrl = linkpth;

                //Response.Redirect("area5info.aspx");

            }
            }
        }

        protected void save_Click(object sender, EventArgs e)
        {
            string id = Label1.Text;
            biz = BizArea.Find(" recid = '" + id + "'", "RECID");

            //desp.Text = biz.BizAreaDescription.ValueString;
            //transparent.Text = biz.BizAreaTransparent.ValueString;
            //FCKeditor_desk.Value = biz.BizAreaDescription.ValueString;
            //FCKeditor_transparent.Value = biz.BizAreaTransparent.ValueString;
            biz.BizAreaDescription = FCKeditor_desk.Value;
            biz.BizAreaTransparent = FCKeditor_transparent.Value;

            biz.Modify();
        }
        protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "delete")
            {
                string id = e.CommandArgument.ToString();
                RuleCode.Table.BizAreaImages images = BizAreaImages.Find(" RECID= " + id);
                //delete image file
                // string temp = ResolveUrl(images.cbimgpath.StringValue);
                //string te= Server.MapPath(temp);
                string fileName = Server.MapPath(ResolveUrl(images.BizAreaImagePath.StringValue));
                if (File.Exists(fileName))
                {
                    File.Delete(fileName);
                    images.Delete();
                    DataListBind(Label1.Text);
                }



                //e.CommandArgument 
            }

        }
    }
}